class CategoryController < ApplicationController
  # TODO avoid duplication (using with_scope ?)
  # TODO optimise the laod of the last reply
  
  def index
    @categories = Category.find(
      :all,
      :order => 'categories.position ASC, forums.position ASC',
      :include => [
        :forums[:name, :position, :description_html, :permalink, :topics_count, :posts_count, :last_reply_id] =>
          [:last_reply[:user_id, :created_at] => :user[:login]]
      ]
    )
    
    respond_to do |format|
      format.html # index.html
      format.xml { render :xml => @categories.to_xml }
    end
  end
  
  def show
    @category = Category.find(
      params[:id],
      :order => 'forums.position ASC',
      :include => [
        :forums[:name, :position, :description_html, :permalink, :topics_count, :posts_count, :last_reply_id] =>
          [:last_reply[:user_id, :created_at] => :user[:login]]
      ]
    )
    
    respond_to do |format|
      format.html # index.html
      format.xml { render :xml => @category.to_xml }
    end
  rescue ActiveRecord::RecordNotFound
    flash[:error] = _('Category not found')
    redirect_back_or(home_path)
  end
end